package com.gpc.hexapods.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RadioButton;

import com.gpc.hexapods.R;

import java.util.List;

public class GridButtonAdapter extends BaseAdapter {

	private LayoutInflater mInflater;
	private ButtonOnClickListener mButtonOnClickListener;
	private List<String> mList;
	private int mCheckNum = 0;
	
	
	public GridButtonAdapter(Context context, ButtonOnClickListener listener, List<String> list) {
		this.mInflater = LayoutInflater.from(context);
		this.mButtonOnClickListener = listener;
		this.mList = list;
	}

	@Override
	public int getCount() {
		return mList.size();
	}

	@Override
	public Object getItem(int position) {
		return mList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		final ViewHolder holder;
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.gridview_list, parent, false);
			holder = new ViewHolder();
			holder.button = (RadioButton) convertView.findViewById(R.id.button);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		if (mCheckNum == position) {
			holder.button.setChecked(true);
		} else {
			holder.button.setChecked(false);
		}
		
		holder.button.setText(mList.get(position));
		holder.button.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				mCheckNum = position;
				mButtonOnClickListener.onClickListener(position + 1);
				notifyDataSetChanged();
			}
		});
		
		return convertView;
	}
	
	public class ViewHolder{
	    public RadioButton button;
    }
	

	public interface ButtonOnClickListener {
		public void onClickListener(int position);
	}
}